<?php

require_once '../util/smurfs.php';

define('DATA_ROOT_DIR','/');

function bitter( $file, $tplvars = array() ) {
  extract($tplvars);
  ob_start();
  $tpl = toTplFile( $file );
  if( file_exists( $tpl ) )
    include( $tpl );
  else
    return $tpl . 'template does non exist'; // XXX error file
  $contents = ob_get_contents();
  ob_end_clean();
  return $contents;
}

function toTplFile($file) {
  global $__CFG;
  return $__CFG['STYLE']['TPL_DIR'] . DIRECTORY_SEPARATOR . $__CFG['STYLE']['NAME'] . DIRECTORY_SEPARATOR . $file . $__CFG['STYLE']['FILE_TYPE'];
}

function showTpl( $file, $tplvars = array() ) {
  echo bitter( $file, $tplvars );
}

function rewrite_dirty($page) {
  $poff = strrpos($page, '/');
  $url = DATA_ROOT_DIR . substr($page,0,$poff) . '.php?page=' . substr($page, $poff+1);
}
function rewrite_clean($page) {
  return $page;
}

function absLink( $page ) {
  return $page;
}
function staticLink( $page, $prefix='' ) {
  return $GLOBALS['__CFG']['BASE_DIR'].$prefix.$page;
}
function realLink( $page, $args = array(), $append_get = false, $prefix='' ) {
  $rewrite_first_sep = '?';
  $rewrite_alt_sep = '&amp;';
  $rewrite_array_assignment = '[]_';
  $rewrite_assignment = '=';

  if(strpos($url,$rewrite_first_sep) === false) $url .= $rewrite_first_sep;
  else $url .= $rewrite_alt_sep;

  $param_arr = array();
  foreach( $args as $key=>$data) {
    if(is_array($data)) {
      foreach($value as $val) {
        $param_arr[] = htmlentities($key) . $rewrite_array_assignment . urlencode(utf8_decode($data));
      } 
    } else {
      $param_arr[] = htmlentities($key) . $rewrite_assignment . urlencode(utf8_decode($data));
    }
  }
  // doubles the O time I'm assuming to implode.
  $url .= implode($rewrite_alt_sep, $param_arr);

  return $GLOBALS['__CFG']['BASE_DIR'].$prefix.$url;
}
function prettyLink( $page, $prettyFormat, $args = array(), $prefix='' ) {
  $null_value = 'null';

  //$url = $page.'/'.implode('/', $args);
  if( !$prettyFormat )
    return $GLOBALS['__CFG']['BASE_DIR'].$prefix.$page;

  $url = $page;
  foreach( $prettyFormat as $pretty ) {
    if( $args[$pretty] )
      $url .= '/'. $args[$pretty];
    else
      $url .= '/'. $null_value;
  }

  // xxx, need a way to turn on the 'view/page' option
  return $GLOBALS['__CFG']['BASE_DIR'].$prefix.$url;
}
function linkAt( $page, $url_type = "static", $args = array(), $append_get = false ) {
  // I motherfucking hate pretty urls :/
  switch( $url_type ) {
    case "abs": // a const url. no dynamic variables, no nuttin
        $url = absLink($page);
        break;
    case "static": // a const url. no dynamic variables, no nuttin
        $url = staticLink($page);
        break;
    case "real": // a normal url
        $url = realLink( $page, $args, $append_get );
        break;
    case "sloppy": //just a quick hack pretty, like original /page/fid_1/key_val
                   // we currently don't have a decoder so this is ignored
        break;
    case "pretty": // per page pretty
        // we should check that pretties index exists
        // may be better to do some global magic if this is needed in more places
        //static $pretties = parse_ini_file('../pageuri.ini');
        // as I somewhat expected php won't allow functions for static vars
        // so we are going to need to mangle with $GLOBALS again
        $url = prettyLink( $page, $GLOBALS['__PAGE_URI'][$page], $args );
        break;
    default:
        $url = "WTF, BROKEN LINK TYPE";
        break;
  }

  return $url;
/*
  $rewrite_rule = 'rewrite_clean'; // or rewrite_dirty
  $rewrite_first_sep = '/';
  $rewrite_alt_sep = '/';
  $rewrite_array_assignment = '[]_';
  $rewrite_assignment = '_'; // or '&amp;'

  if($append_get) $params += $_GET;
  if($real_url) {
    $url = $page;
  } else {
    $url = $rewrite_rule($page);
  }
  if(!$args) return $url;

  if(strpos($url,$rewrite_first_sep) === false) $url .= $rewrite_first_sep;
  else $url .= $rewrite_alt_sep;

  $param_arr = array();
  foreach( $args as $key=>$data) {
    if(is_array($data)) {
      foreach($value as $val) {
        $param_arr[] = htmlentities($key) . $rewrite_array_assignment . urlencode(utf8_decode($data));
      } 
    } else {
      $param_arr[] = htmlentities($key) . $rewrite_assignment . urlencode(utf8_decode($data));
    }
  }
  // doubles the O time I'm assuming to implode.
  $url .= implode($rewrite_alt_sep, $param_arr);

  return $url;*/
} 

function decode_uri( &$data, $pass, $prettyFormat ) {
// just going to be the oppoiste of linkAt pretty, where we take out based off the uri array, rather then put in.

  foreach( $prettyFormat as $pretty )
    $data[$pretty] = array_shift($pass);

}

function handlePage($page, $type="static") {
  switch( $type ) {
    case "abs": // a const url. no dynamic variables, no nuttin
        $url = absLink($page);
        break;
    case "static": // a const url. no dynamic variables, no nuttin
        $url = staticLink($page, 'handle/');
        break;
    case "real": // a normal url
        $url = realLink( $page, $args, $append_get, 'handle/' );
        break;
    case "sloppy": //just a quick hack pretty, like original /page/fid_1/key_val
                   // we currently don't have a decoder so this is ignored
        break;
    case "pretty": // per page pretty
        // we should check that pretties index exists
        // may be better to do some global magic if this is needed in more places
        //static $pretties = parse_ini_file('../pageuri.ini');
        // as I somewhat expected php won't allow functions for static vars
        // so we are going to need to mangle with $GLOBALS again
        $url = prettyLink( $page, $GLOBALS['__PAGE_URI'][$page], $args, 'handle/' );
        break;
    default:
        $url = "WTF, BROKEN LINK TYPE" . $type;
        break;
  }

  return $url;
}

// don't know exactly how smart this is, experimental
function start_smurfing() {
  ob_start();  
}
function end_smurfing() {
  echo desmurf(ob_get_contents(), $smurfkey);
}

?>
